#include<string>
#include<vector>
using namespace std;

class Solution {
private:
    // 阶乘函数
    int factorial(int x) {
        int res = 1;
        for (int i = 2; i <= x; ++i) res *= i;
        return res;
    }
    
public:
    string getPermutation(int n, int k) {
        vector<int> nums;
        for (int i = 1; i <= n; ++i) nums.push_back(i);

        string res = "";
        k--;  // 转为0索引
        while (n > 0) {
            int cur = factorial(n - 1);
            int idx = k / cur;
            k %= cur;
            res += to_string(nums[idx]);
            nums.erase(nums.begin() + idx);
            n--;
        }
        return res;
    }
};
